package whf.framework.web.tag;

import java.util.List;

import javax.servlet.jsp.JspException;

import whf.framework.entity.TreeNode;
import whf.framework.entity.ext.TreeItem;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.meta.MetaFactory;
import whf.framework.service.Service;
import whf.framework.service.TreeService;
import whf.framework.util.BeanFactory;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;
import whf.framework.web.WebContextHolder;

/**
 * 菜单tag，具体的使用alai菜单实现
 * @author wanghaifeng
 * @create Jan 21, 2007 5:45:42 PM
 * 
 */
public class MenuTag extends HtmlTag{
	private static Log log = LogFactory.getLog(MenuTag.class);
	
	private String queryString;
	
	private String boClassName;
	
	private long rootId;
	
	/**
	 * 菜单点击js函数，至少两个参数：menuid, deepth
	 * @property String:nodeOnclickFunction
	 */
	private String nodeOnclickFunction;
	
	private String menuBarProperties;
	
	private boolean cacheData;
	
	public void setBoClassName(String name){
		this.boClassName = name;
	}
	
	public void setRootId(long id){
		this.rootId = id;
	}
	
	public void setNodeOnclickFunction(String nodeOnclickFunction) {
		this.nodeOnclickFunction = nodeOnclickFunction;
	}

	public void setQueryString(String str){
		this.queryString = str;
	}
	
	public void setMenuBarProperties(String bar){
		this.menuBarProperties = bar;
	}
	
	public Service getService() throws Exception{
		return BeanFactory.getService(MetaFactory.findByModelClass(Utils.getClassByClassName(boClassName)));
	}
	
	public void setCacheData(boolean b){
		this.cacheData = b;
	}
	
	public int doStartTag() throws JspException{
		//确定根结点
		TreeNode root = null;
		if(this.rootId != 0){
			try{
				root = (TreeNode)getService().findByPrimaryKey(rootId);
			}catch(Exception e){
				log.error(this, e);
			}
		}
		//确定树节点数据
		String key = "Menu:"+boClassName;
		if(!StringUtils.isEmpty(this.queryString)){
			key += "#" + this.queryString;
		}
		key += "@"+TreeItem.class.getName();
		TreeItem treeItem = null;
		if(this.cacheData){
			treeItem = (TreeItem)WebContextHolder.getWebContext().getUserContext().getSessionObject(key);
		}
		if(treeItem == null){
			try{
				if(root != null){
					treeItem = new TreeItem(root, (TreeService)this.getService(), this.queryString, -1);
				} else {
					treeItem = new TreeItem(this.boClassName);
				}
			}catch(Exception e){
				log.error(this, e);
				throw new JspException(e);
			}
			if(this.cacheData){
				WebContextHolder.getWebContext().getUserContext().setSessionObject(key, treeItem);
			}
		}
		//写节点数据
		super.println("<script language='jscript'>");
		super.println("var menubar=new menu_bar_xp("+(this.menuBarProperties == null?"":this.menuBarProperties)+");");
		List<TreeItem> items = treeItem.getChildren();
		for(TreeItem item: items){
			this.writeTreeItem(item);
		}
		super.println("</script>");
		//
		this.release();
		return SKIP_BODY;
	}
	
	private void writeTreeItem(TreeItem item) throws JspException{
		TreeItem parent = (TreeItem)item.getParent();
		List<TreeItem> children = item.getChildren();
		super.println("");
		if(children != null && !children.isEmpty() || item.getDeepth() == 1){	//非功能菜单项
			super.println("var m"+item.getId()+" = new alai_menu_xp();");
			if(item.getDeepth() == 1 || parent == null){	//加入到menubar上
				super.println("menubar.add(\""+item.getName()+"\",m"+item.getId()+")");
			} else {
				super.println("m"+parent.getId()+".add(\""+item.getName()+"\",\""+item.getIcon()+"\",\"sub\",m"+item.getId()+")");
			}
		} else {
			super.println("m"+parent.getId()+".add(\""+item.getName()+"\",\""+item.getIcon()+"\",\"js\",\""+this.nodeOnclickFunction+"('"+item.getName()+"','"+item.getId()+"','"+item.getDeepth()+"');\") ");
		}
		
		if(children != null && children.size() > 0){
			for(TreeItem child: children){
				this.writeTreeItem(child);
			}
		}
	}
	
	public void release(){
		super.release();
		this.boClassName = null;
		this.menuBarProperties = null;
		this.queryString = null;
		this.rootId = 0;
		this.nodeOnclickFunction = null;
		this.cacheData = false;
	}
}
